<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 找回密码控制器
 * @author LM
 * @editor Calvin<hongbinfu@163.com>
 */
class Find_pwd extends CI_Controller{
	private $_sms_step1="__FIND_PWD_VALID_MOBILE__";
	private $_sms_step2="__FIND_PWD_RESET_PWD__";
	private $_find_pwd_vcode = "__FIND_PWD_VCODE__";
	public function __construct(){
		parent::__construct();
		$this->load->service('login_service');
		$this->load->service('user_verify_service');
		$this->load->service('user_info_service');
		$this->load->library("form_validation");
		$this->config->load("form_validation");
		//$this->load->model('user_model');
	}

	private function reset_step_session()
	{
		$this->session->unset_userdata($this->_sms_step1);
		$this->session->unset_userdata($this->_sms_step2);
	}
	public function code()
	{
		$config = array(
			'width'	=>	80,
			'height'=>	34,
			'codeLen'=>	4,
			'fontSize'=>16
		);
		$this->load->library('code', $config);
		$this->session->set_userdata($this->_find_pwd_vcode, $this->code->getCode());
		$this->code->show();
	}
	//找回密码视图
	public function index(){
		$this->reset_step_session();

		//生成唯一标识符
		$data = array(
				'title' => '找回密码'
				);
		$this->template->view('user/find_pwd.tpl', $data);
	}
	
	//邮箱判断
	public function useremail_check($email){
		$userData = $this->user_verify_service->check_user_by_email($email);
		if(!$userData || $userData['verify'] == 0){
			$this->form_validation->set_message('useremail_check', '帐号不存在！');
			return false;
		} else {
			return true;
		}
	}
	//找回密码表单处理
	public function do_find_pwd()
	{
		$vcode = $this->input->post('vcode',TRUE);
		$account = $this->input->post('account',TRUE);
		if(empty($account) || empty($vcode))
		{
			return Common::ajax_return(1003);
		}
		if(strtoupper($vcode)!= $this->session->userdata($this->_find_pwd_vcode))
		{
			return Common::ajax_return(1007);
		}
		$valid_email = $this->form_validation->valid_email($account);
		$valid_mobile = $this->form_validation->valid_mobile_phone($account);
		if(!$valid_email && !$valid_mobile)
		{
			return Common::ajax_return(1003);
		}


		if($valid_email)
		{
			//$check = $this->user_verify_service->can_send_email($account,User_verify_service::PROCESS_FIND_PASSWORD,"");
			$check_user = $this->user_verify_service->check_user_by_email($account);
			//检查用户
			if($check_user===FALSE)
			{
				return Common::ajax_return($this->user_verify_service);
			}
		}
		else
		{
			//$check  = $this->user_verify_service->can_send_sms($account,2);
			$check_user = $this->user_verify_service->check_user_by_phone($account);
			//检查用户
			if($check_user===FALSE)
			{
				return Common::ajax_return($this->user_verify_service);
			}
		}


		/*if(!$check){
			return Common::ajax_return($this->user_verify_service);
		}*/
		//是否发送链接式验证邮箱
		//$send_link = $this->input->post("send_link");
		if($valid_email)
		{
			$send = $this->user_verify_service->send_email($account,User_verify_service::PROCESS_FIND_PASSWORD,"find_pwd/reset_pwd");
		}
		else
		{
			$send = $this->user_verify_service->send_sms($account,User_verify_service::PROCESS_FIND_PASSWORD);
		}

		if($send)
		{
			if($valid_email)
			{
				return Common::ajax_return(0,NULL,array("account_type"=>1,"redirect"=>"/find_pwd/send_email_success/".urlencode($account)),FALSE);
			}
			else
			{
				$this->session->set_userdata($this->_sms_step1,array("mobile"=>$account,"time"=>time()));
				return Common::ajax_return(0,NULL,array("account_type"=>2,"redirect"=>"/find_pwd/valid_mobile"),FALSE);
			}
		}
		else
		{
			return Common::ajax_return($this->user_verify_service);
		}
	}

	/**
	 * 验证手机页面
	 */
	public function valid_mobile()
	{
		$data = $this->session->userdata($this->_sms_step1);
		if(empty($data))
		{
			redirect("/find_pwd");
		}
		$sms_code_len=$this->config->item("sms_verify_code_length","user_common");
		//生成唯一标识符
		$view = array(
			'title' => '找回密码',
			'mobile'=> $data['mobile'],
			'sms_code_len'=>$sms_code_len
		);
		//$this->session->unset_userdata("__FIND_PWD_VALID_MOBILE__");
		$this->template->view('user/find_pwd_valid_mobile.tpl', $view);
	}

	/**
	 * 重新发送验证码
	 */
	public function resend_sms()
	{
		$data = $this->session->userdata($this->_sms_step1);
		if(empty($data))
		{
			show_404();
		}

		$send = $this->user_verify_service->send_sms($data['mobile'],User_verify_service::PROCESS_FIND_PASSWORD);
		if($send==FALSE)
		{
			return Common::ajax_return($this->user_verify_service);
		}
		else
		{
			return Common::ajax_return();
		}
	}

	/**
	 * 检查短信验证码
	 */
	public function check_sms_code()
	{
		$mobile = $this->input->post("mobile",TRUE);
		$code = $this->input->post("code",TRUE);
		if(empty($mobile) || empty($code))
		{
			return Common::ajax_return(1003);
		}
		$check = $this->user_verify_service->check_sms_code($code,$mobile,2,FALSE);
		if($check===FALSE)
		{
			return Common::ajax_return($this->user_verify_service);
		}
		else
		{
			$this->session->set_userdata("__FIND_PWD_RESET_PWD__",array("mobile"=>$mobile,"code"=>$code,"time"=>time()));
			return Common::ajax_return(0,NULL,array("redirect"=>"/find_pwd/reset_pwd_by_sms"),FALSE);
		}
		/*//生成唯一标识符
		$data = array(
			'title' => '找回密码',
			'mobile'=> $mobile
		);
		//$this->session->unset_userdata("__FIND_PWD_VALID_MOBILE__");
		$this->smarty->view('user/find_pwd_valid_mobile.tpl', $data);*/
	}

	/**
	 * 短信方式找回密码重置密码界面
	 */
	public function reset_pwd_by_sms()
	{
		$data = $this->session->userdata($this->_sms_step2);
		if(empty($data))
		{
			redirect("/find_pwd");
		}
		$this->template->assign('title', '重置密码');
		$this->template->view('user/reset_pwd_by_sms.tpl',$data);
	}

	public function do_reset_pwd_by_sms()
	{
		$pwd =  $this->input->post('password',TRUE);
		$mobile =  $this->input->post('mobile',TRUE);
		$code = $this->input->post("code",TRUE);
		if(empty($pwd) || !$this->form_validation->valid_password($pwd) || empty($code) || empty($mobile))
		{
			return Common::ajax_return(1003);
		}
		//校验验证码并销毁
		$check_code = $this->user_verify_service->check_sms_code($code,$mobile,User_verify_service::PROCESS_FIND_PASSWORD,TRUE);
		if($check_code===FALSE)
		{
			return Common::ajax_return($this->user_verify_service);
		}
		$state = $this->user_info_service->update_user_info_by_phone($mobile, array('password' => md5($pwd)));
		if($state)
		{
			$this->reset_step_session();
			$this->login_service->logout();
			return Common::ajax_return(0,NULL,array('redirect'=>"/find_pwd/reset_success"),FALSE);
		}
		else
		{
			return Common::ajax_return($this->user_info_service);
		}
	}

	private function show_result($msg,$title="错误提示",$type="error")
	{
		$data['msg'] = $msg;
		$data['title'] = $title;
		$data['type'] = $type;
		$this->template->view('common/tips.tpl', $data);
	}
	/**
	 * 重新发送
	 */
	public function reset_send(){
		$email = $this->input->get_post('email');
		$check = $this->user_verify_service->check_user_by_email($email);
		if($check==FALSE)
		{
			return Common::ajax_return($this->user_verify_service);
		}
		if(!$this->user_verify_service->send_email($email,User_verify_service::PROCESS_FIND_PASSWORD,"find_pwd/reset_pwd")){
			/*$error = $this->user_verify_service->get_error();
			self::ret_error($error['code'],$error['msg']);*/
			return Common::ajax_return($this->user_verify_service);
		}
		return Common::ajax_return();
	}

	/**
	 * 邮件发送成功
	 */
	public function send_email_success($email=""){
		$email = !empty($email)?$email:$this->input->get("email");
		if(empty($email))
		{
			show_404();
		}
		$email = urldecode($email);
		$to_email_url = Common::get_email_url($email);
		$this->template->assign('email', $email);
		$this->template->assign('to_email_url', $to_email_url);
		$this->template->assign('title', '找回密码');
		$this->template->view('user/send_succ.tpl');
	}

	/**
	 * 邮件发送失败
	 */
	public function send_email_error($email=""){
		$email = !empty($email)?$email:$this->input->get("email");
		if(empty($email))
		{
			show_404();
		}
		$this->load->helper('form');
		$data['email'] = set_value('email');
		$data['email_error'] = "发送失败";
		$data['title'] = '找回密码';
		$this->template->view('user/find_pwd.tpl', $data);
		return false;
	}
	//重置密码视图
	public function reset_pwd($code=""){
		if(empty($code))
		{
			show_404();
		}
		//校验验证码不销毁
		$result = $this->user_verify_service->check_email_verify_code($code,"",User_verify_service::PROCESS_FIND_PASSWORD,TRUE,FALSE);
		if($result===false){
			$data = array(
					'title' => '找回密码',
					'model' => 'find_pwd'
					);
			$this->template->view('user/tips.tpl',$data);
			return false;
		}
		$this->template->assign('title', '重置密码');
		$this->template->assign('code', $code);

		$this->template->view('user/reset_pwd.tpl');
	}

	//重置密码表单处理
	public function do_reset_pwd(){

		$this->form_validation->set_rules('code','', 'required');
		$this->form_validation->set_rules('password','', 'required|valid_password');
		if($this->form_validation->run()==FALSE)
		{
			return Common::ajax_return(1003);
		}
		$pwd =  $this->input->post('password',TRUE);
		$code = $this->input->post("code",TRUE);
		//校验验证码并销毁
		$check_code = $this->user_verify_service->check_email_verify_code($code,"",User_verify_service::PROCESS_FIND_PASSWORD,TRUE,TRUE);
		if($check_code===FALSE)
		{
			return Common::ajax_return($this->user_verify_service);
		}
		$state = $this->user_info_service->update_user_info_by_email($check_code['receiver'], array('password' => md5($pwd)));
		if($state)
		{
			$this->login_service->logout();
			//Common::success('/login', '重置密码成功');
			return Common::ajax_return(0,NULL,array('redirect'=>"/find_pwd/reset_success"),FALSE);
		}
		else
		{
			return Common::ajax_return($this->user_info_service);
			//Common::error('重置密码失败');
		}

	}
	public function reset_success()
	{
		$data=array(
			"title"=>"密码重置成功",
			"type" =>"success",
			"msg" =>"你的密码已经重置成功"
		);
		$this->template->view("user/reset_pwd_success.tpl",$data);
	}

	/**
	 * 错误处理
	 */
	static function ret_error($code = 1, $error = '', $data = null){
		$data = array(
			'ret'       => $code,
			'error'     => $error,
			'data'      => $data
		);
		echo json_encode($data);
		die();
	}
}